import javax.swing.*;
import java.awt.*;

class Analysis extends JFrame {
    private FamilyFinanceManager mainFrame;
    private JButton backButton; // 将 backButton 声明为类的成员变量

    public Analysis(FamilyFinanceManager mainFrame) {
        this.mainFrame = mainFrame;
        setTitle("统计分析");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);

        JTextArea analysisTextArea = new JTextArea();
        analysisTextArea.setEditable(false);

        // 构建统计分析内容
        StringBuilder sb = new StringBuilder();
        sb.append("这里是统计分析结果\n");
        sb.append("总收入: ").append(DataManager.getTotalIncome()).append("\n");
        sb.append("总支出: ").append(DataManager.getTotalExpense()).append("\n");
        sb.append("结余: ").append(DataManager.getBalance()).append("\n\n");

        sb.append("收入明细:\n");
        for (String income : DataManager.getIncomes()) {
            sb.append(income).append("\n");
        }

        sb.append("\n支出明细:\n");
        for (String expense : DataManager.getExpenses()) {
            sb.append(expense).append("\n");
        }

        sb.append("\n预算设定:\n");
        for (String budget : DataManager.getBudgets()) {
            sb.append(budget).append("\n");
        }

        analysisTextArea.setText(sb.toString());

        JScrollPane scrollPane = new JScrollPane(analysisTextArea);

        add(scrollPane, BorderLayout.CENTER);

        backButton = new JButton("返回"); // 初始化成员变量
        add(backButton, BorderLayout.SOUTH);

        addListeners();
    }

    private void addListeners() {
        backButton.addActionListener(e -> {
            mainFrame.setVisible(true);
            dispose();
        });
    }
}